import type {
  KnowledgePage,
  KnowledgeParams,
  DoctorPage,
  PageParams,
  FollowType,
  TopDep,
  Image,
  PartialConsult,
  ConsultOrderItem,
  ConsultOrderListParams,
  ConsultOrderPage
} from '@/types/consult'

import type {
  ConsultOrderPreData,
  ConsultOrderPreParams
} from '@/types/consult'
import { Patient } from '@/types/user'

import { request } from '@/utils/request'

/**
 * 获取知识列表
 * @param params
 */
export const getKnowledgePage = (params: KnowledgeParams) => {
  return request<KnowledgePage>('/patient/home/knowledge', 'GET', params)
}

/**
 * 获取医生列表
 * @param params
 */
export const getDoctorPage = (params: PageParams) => {
  return request<DoctorPage>('/home/page/doc', 'GET', params)
}

/**
 * 关注|取消关注
 * @param id
 * @param type
 */
export const followOrUnfollowApi = (id: string, type: FollowType) => {
  return request('/like', 'post', { id, type })
}

export const getAllDep = () => request<TopDep[]>('/dep/all')

/**
 * 上传图片
 * @param file
 */
export const uploadImage = (file: File) => {
  const fd = new FormData()
  fd.append('file', file)
  return request<Image>('/upload', 'POST', fd)
}
// 拉取预支付订单信息
export const getConsultOrderPre = (params: ConsultOrderPreParams) =>
  request<ConsultOrderPreData>('/patient/consult/order/pre', 'GET', params)

/**
 * 查询患者详情
 * @param id
 */
export const getPatientDetail = (id: string) =>
  request<Patient>(`/patient/info/${id}`)

/**
 * 创建订单
 * @param data
 */
export const createConsultOrder = (data: PartialConsult) => {
  return request<{ id: string }>('/patient/consult/order', 'POST', data)
}

// 获取支付地址  0 是微信  1 支付宝
export const getConsultOrderPayUrl = (params: {
  paymentMethod: 0 | 1
  orderId: string
  payCallback: string
}) => request<{ payUrl: string }>('/patient/consult/pay', 'POST', params)

/**
 * 获取订单详情
 * @param orderId 订单id
 */
export const getConsultOrderDetail = (orderId: string) =>
  request<ConsultOrderItem>('/patient/consult/order/detail', 'GET', { orderId })

/**
 * 获取未读消息
 */
export const getUnReadMsgApi = () => {
  return request<number>('/patient/message/unRead/all')
}

/**
 * 获取原始处方信息
 * @param id 处方id
 */
export const getPrescriptionPicApi = (id: string) => {
  return request<{ url: string }>(`/patient/consult/prescription/${id}`)
}

/**
 * 评价医生
 * @param data 评价医生的参数
 */
export const evaluateConsultOrder = (data: {
  docId: string
  orderId: string
  score: number
  content: string
  anonymousFlag: 0 | 1
}) => {
  return request('/patient/order/evaluate', 'POST', data)
}

/**
 * 获取订单类型
 * @param params
 */
export const getConsultOrderList = (params: ConsultOrderListParams) => {
  return request<ConsultOrderPage>('patient/consult/order/list', 'get', params)
}

/**
 * 取消订单
 * @param id
 */
export const cancelOrder = (id: string) =>
  request(`/patient/order/cancel/${id}`, 'PUT')

/**
 * 删除订单
 * @param id 订单id
 */
export const deleteOrder = (id: string) =>
  request(`/patient/order/${id}`, 'DELETE')
